package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.model.Find;
import com.rongke.model.Goods;
import com.rongke.service.FindService;
import com.rongke.service.GoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @FindController
 * @发现（废弃）Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/find")
@Transactional
@CrossOrigin(origins = "*", maxAge = 3600)
public class FindController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private FindService findService;
    @Autowired
    private GoodsService goodsService;
    /**
     * @添加发现（）
     * @param find
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addFind(@RequestBody Find find){
        log.debug("添加发现（）");
        Goods goods = goodsService.selectById(find.getGoodsId());
        if (goods == null) {
            return JsonResp.toFail("商品Id不存在");
        }else{
            findService.insert(find);
            return JsonResp.ok(find);
        }


    }

    /**
     * @修改发现（）
     * @param find
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateFind(@RequestBody Find find){
        log.debug("修改发现（）");
        findService.updateById(find);
        return JsonResp.ok(find);
    }
    /**
     * @根据id查找发现（）
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectFind(Long id){
        log.debug("查找发现（）");
        Find find = findService.selectById(id);
        return JsonResp.ok(find);
    }

    /**
     * @根据条件查找发现（）
     * @param find
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectFindByCondition(@RequestBody Find find){
        log.debug("根据条件查找发现（）");
        if(find==null)
            return JsonResp.toFail("参数不能为空");
        List<Find> findList=findService.selectByCondition(find);
        return JsonResp.ok(findList);
    }


    /**
     * 首页显示
     * @return 返回值JsonResp
     * @param page
     * @遍历问卷
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public JsonResp selectAll(Page page) {
        log.debug("查找主页问卷列表");
        EntityWrapper<Find> qt = new EntityWrapper<>();
        Page<Find> goodsPage = findService.selectPage(page, qt);
        return JsonResp.ok(goodsPage);
    }


    /**
     * @删除
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/delete", method = RequestMethod.GET)
    public JsonResp delete(Long id){
        log.debug("删除");
        findService.deleteById(id);
        return JsonResp.ok();
    }
}
